package org.example.order.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.comment.feign.LogisticFeign;
import org.example.constant.MessageConstant;
import org.example.dto.OrderDto;
import org.example.entity.Order;
import org.example.entity.Product;
import org.example.comment.feign.ProductFeign;
import org.example.entity.UserAddresses;
import org.example.order.exception.StackShoutException;
import org.example.order.mapper.OrderMapper;
import org.example.order.service.OrderService;
import org.example.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanfutian
 * @since 2025-04-23
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private LogisticFeign logisticFeign;


    @Transactional
    @Override
    public void addOrder(String userId, OrderDto orderDto){
        // 远程调用商品服务，获取商品信息
        Long productId = orderDto.getProductId();
        Result<Product> productData = productFeign.getProduct(productId);
        log.info("ProductFeign成功！{}",productData.getData());
        Product product = productData.getData();
        Order order = new Order();
        order.setUserId(Long.parseLong(userId));
        order.setProductId(product.getId());
        order.setProductName(product.getProductName());

        // 设置商品价格(数量乘以商品价格)
        BigDecimal productPrice = product.getProductPrice();
        Integer productCount = orderDto.getProductCount();
        order.setProductCount(productCount);
        order.setProductPrice(productPrice.multiply(new BigDecimal(productCount)));
        order.setCreateTime(LocalDateTime.now());

        // 获取地址信息
        Result<List<UserAddresses>> userAddresses = logisticFeign.getUserAddresses(Long.valueOf(userId));
        log.info("logisticFeign成功！{}",userAddresses.getData());
        for(int i = 0; i <= userAddresses.getData().size()-1 ; i++){
            UserAddresses userAddresses1 = userAddresses.getData().get(i);
            if(userAddresses1.getIsDefault()){
                order.setAddressId(userAddresses1.getAddressId());
            }
        }

        // 判断商品数量是否足够
        if(!(product.getProductStock() >= orderDto.getProductCount())){
            throw new StackShoutException(MessageConstant.STACK_COUNT_SHORT);
        }

        // 生成订单
        orderMapper.insertOrder(order);

        // 修改库存数量
        product.setProductStock(product.getProductStock() - productCount);
        productFeign.updateProduct(product);
    }


    @Override
    public List<Order> getOrderByUserId(Long userId) {
        List<Order> orders = orderMapper.selectByUserId(userId);
        log.info("根据用户id：{} 获取订单数据成功!",userId);
        return orders;
    }

    @Override
    public Order getOrderByOrderId(Long orderId) {
        Order order = orderMapper.selectByOrderId(orderId);
        log.info("根据订单id：{} 获取订单数据成功！{}",orderId,order);
        return order;
    }
}
